using System;

namespace Sedna.Core.Common.Objects.Navigation
{
    /// <summary>
    /// Represents parsed node in the navigation expression.
    /// </summary>
    [Serializable]
    internal class NumericLiteralNode : NavigationNode
    {
        private object nodeValue;

        /// <summary>
        /// Creates instance of the node.
        /// </summary>
        /// <param name="parent">Parent node.</param>
        /// <param name="nodeExpression">This node's expression.</param>
        public NumericLiteralNode(NavigationNode parent, string nodeExpression) : base(parent, nodeExpression, null)
        {
            if (nodeExpression.IndexOf(".") > 0)
            {
                nodeValue = Double.Parse(nodeExpression);
            }
            else
            {
                try
                {
                    nodeValue = Int32.Parse(nodeExpression);
                }
                catch (OverflowException)
                {
                    nodeValue = Int64.Parse(nodeExpression);
                }
            }
        }

		protected override object GetValueRecursive(bool autoCreate)
		{
            return nodeValue;
        }

        /// <summary>
        /// Sets node's value for the given context.
        /// </summary>
        /// <param name="newValue">New value for this node.</param>
        protected override void SetValue(object newValue)
        {
            throw new NotSupportedException("Can't set the value of the numeric literal.");
        }
    }
}